package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.util.Assert;
import com.atguigu.srb.core.enums.BorrowInfoStatusEnum;
import com.atguigu.srb.core.enums.BorrowerStatusEnum;
import com.atguigu.srb.core.mapper.BorrowerMapper;
import com.atguigu.srb.core.mapper.IntegralGradeMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.BorrowInfo;
import com.atguigu.srb.core.mapper.BorrowInfoMapper;
import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.BorrowInfoApprovalVO;
import com.atguigu.srb.core.pojo.vo.BorrowerDetailVO;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.service.BorrowInfoService;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.core.service.DictService;
import com.atguigu.srb.core.service.LendService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 借款信息表 服务实现类
 * </p>
 *
 * @author fan
 * @since 2021-11-22
 */
@Service
public class BorrowInfoServiceImpl extends ServiceImpl<BorrowInfoMapper, BorrowInfo> implements BorrowInfoService {
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private IntegralGradeMapper integralGradeMapper;
    @Resource
    private DictService dictService;
    @Resource
    private BorrowerService borrowerService;
    @Resource
    private LendService lendService;
    @Override
    public BigDecimal getBorrowAmount(String userId) {
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("id",userId);
        UserInfo userInfo = userInfoMapper.selectOne(userInfoQueryWrapper);
        Integer integral = userInfo.getIntegral();

        // 根据积分获得借款额度
        QueryWrapper<IntegralGrade> integralGradeQueryWrapper = new QueryWrapper<>();
        // integral_start<=用户积分<=integral_end
        integralGradeQueryWrapper
                .le("integral_start",integral)
                .gt("integral_end",integral);
        IntegralGrade integralGrade = integralGradeMapper.selectOne(integralGradeQueryWrapper);

        return integralGrade.getBorrowAmount();
    }



    @Override
    public Integer getBorrowInfoStatus(String userId) {
        QueryWrapper<BorrowInfo> borrowInfoQueryWrapper = new QueryWrapper<>();
        borrowInfoQueryWrapper.eq("user_id",userId);
        BorrowInfo borrowInfo = baseMapper.selectOne(borrowInfoQueryWrapper);
        if(null==borrowInfo){
            return 0;
        }else {
            return borrowInfo.getStatus();
        }

    }

    @Override
    public void saveBorrowInfo(BorrowInfo borrowInfo, String userId) {
        borrowInfo.setUserId(userId);
        borrowInfo.setBorrowYearRate(borrowInfo.getBorrowYearRate().divide(new BigDecimal("100")));
        borrowInfo.setStatus(BorrowInfoStatusEnum.CHECK_RUN.getStatus());

        baseMapper.insert(borrowInfo);
    }

    @Override
    public List<BorrowInfo> getBorrowInfoList() {
        List<BorrowInfo> borrowInfoList = baseMapper.selectBorrowInfoList();
        borrowInfoList.forEach(borrowInfo -> {
            String returnMethod = dictService.getNameByParentDictCodeAndValue("returnMethod", borrowInfo.getReturnMethod());
            String moneyUse = dictService.getNameByParentDictCodeAndValue("moneyUse", borrowInfo.getMoneyUse());
            String status = BorrowInfoStatusEnum.getMsgByStatus(borrowInfo.getStatus());
            borrowInfo.getParam().put("returnMethod", returnMethod);
            borrowInfo.getParam().put("moneyUse", moneyUse);
            borrowInfo.getParam().put("status", status);
        });

        return borrowInfoList;
    }

    @Override
    public Map<String, Object> getBorrowInfoDetail(Long id) {
        //封装param到borrowInfo
        //封装borrower、borrowInfo到getBorrowInfoDetail
        BorrowInfo borrowInfo = baseMapper.selectById(id);
        HashMap<String, Object> param = new HashMap<>();
        Integer moneyUse = borrowInfo.getMoneyUse();
        Integer returnMethod = borrowInfo.getReturnMethod();
        Integer status = borrowInfo.getStatus();
        String moneyUseName = dictService.getNameByParentDictCodeAndValue("moneyUse", moneyUse);
        String returnMethodName = dictService.getNameByParentDictCodeAndValue("returnMethod", returnMethod);
        String statusName = BorrowerStatusEnum.getMsgByStatus(status);
        param.put("moneyUse",moneyUseName);
        param.put("returnMethod",returnMethodName);
        param.put("status",statusName);
        borrowInfo.setParam(param);

        BorrowerDetailVO borrowerDetailVO = new BorrowerDetailVO();
        // 根据userId查询borrowerId
        String userId = borrowInfo.getUserId();
        QueryWrapper<Borrower> borrowerQueryWrapper = new QueryWrapper<>();
        borrowerQueryWrapper.eq("user_id",userId);
        Borrower borrower = borrowerService.getOne(borrowerQueryWrapper);
        borrowerDetailVO = borrowerService.getBorrowerDetail(borrower.getId());

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("borrowInfo",borrowInfo);
        resultMap.put("borrower",borrowerDetailVO);

        return resultMap;
    }

    @Override
    public void approval(BorrowInfoApprovalVO borrowInfoApprovalVO) {
        BorrowInfo borrowInfo = baseMapper.selectById(borrowInfoApprovalVO.getId());
        borrowInfo.setStatus(borrowInfoApprovalVO.getStatus());
        baseMapper.updateById(borrowInfo);

        if (borrowInfoApprovalVO.getStatus().intValue() == BorrowInfoStatusEnum.CHECK_OK.getStatus().intValue()) {
            lendService.createLend(borrowInfoApprovalVO);
        }

    }
}
